<?php
abstract class Core_Block_Abstract extends Core_Model_Object {

	protected $_childs = array();
	
	protected $_parent;
	
	public function __construct($option=array()) {
		$this->addData($option);
	}

	public function getChild($name='') {
		$result = null;
		if ($name=='') {
			$result = array();
			foreach ($this->_childs as $c)
				$result[] = $this->getLayout()->getBlock($c);
		} else if (in_array($name, $this->_childs)) {
			$result = $this->getLayout()->getBlock($name);
		}
		return $result;
	}
	
	public function addChild($name) {
		$this->_childs[] = $name;
		$block = $this->getLayout()->getBlock($name);
		return $this;
	}
	
	public function getLayout() {
		return Mag::getController()->getLayout();
	}

	public function removeChild($name) {
		if(($key = array_search($name, $this->_childs)) !== false) {
			$this->getLayout()->removeBlock($name);
			unset($this->_childs[$key]);
		}
		return $this;
	}
	
	final public function getOutput() {
		$this->_beforeToHtml();
		$html = $this->_toHtml();
		$html = $this->_afterToHtml($html);
		return $html;
	}
	
	public function getChildOutput($name) {
		if ($block = $this->getChild($name))
			return $block->getOutput();
		return '';
	}
	
	protected function _beforeToHtml() {
		return $this;
	}
	
	protected function _afterToHtml($html) {
		return $html;
	}

	protected function _toHtml() {
		return '';
	}
	
	public function getSkinUrl($file) {
		return Mag::getController()->getArea()->getCurrentTheme()->getSkinUrl($file);
	}
}